package com.jet.multichat.server;

import java.io.IOException;
import java.net.Socket;

/**
 * 
 * �����, ����������� ���������� �������
 * 1) �������� �� ������� ����� � ������
 * 2) �������� �������������� ������������
 *
 */

public class ChatAccessService {
	private String login;
	private Socket socket;
	
	ChatAccessService(Socket socket) {
		this.socket = socket;
	}

	public String getLogin() {
		return login;
	}

	public void setLogin(String login) {
		this.login = login;
	}
	
	public boolean login() {
		boolean access = false;
		
		try {
			new ChatMessageService("server > Enter login").send(socket);
			ChatMessageService login = new ChatMessageService(socket);

			new ChatMessageService("server > Enter password").send(socket);
			ChatMessageService password = new ChatMessageService(socket);

			access = new ChatAuthentification().autentificate(login.getContent(), password.getContent());
			//access = accessStub(login.getContent(), password.getContent());

		} catch (IOException e) {
			//[ek] TODO will user know about error?
			e.printStackTrace();
		}
		return access;
	}

	private boolean accessStub(String login, String password) throws IOException {
		
		//[ek] TODO WTF?
		//stub
		if (login.equals(password)) {
			this.setLogin(login);
			
			return true;
		} else {
			new ChatMessageService("server > wrong login-password entered. retype").send(socket);
		}

		return false;
	}

}
